package com.xunshangwang.pinhaohuo.http;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.xunshangwang.pinhaohuo.app.MainApplication;
import com.xunshangwang.pinhaohuo.tools.DebugHelper;
import com.xunshangwang.pinhaohuo.ui.activities.MainActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.SocketTimeoutException;

import cz.msebera.android.httpclient.Header;
import tool.library.ToastTool;

/**
 * Created by xzp on 15/10/9.
 */
public abstract class JsonHttpHandler<T> extends BaseJsonHttpResponseHandler<T>{
    public static final String STATUS_KEY="result";
    public static final String STATUS_SUCCESS="ok";
    private static Context mContext;
    public static final void init(Context context){
        mContext=context;
    }
    @Override
    public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, T errorResponse) {
        DebugHelper.e("onFailure", throwable.toString());
        if(throwable instanceof AuthError){
            Intent intent=new Intent(mContext,MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.putExtra("AuthError",true);
            mContext.startActivity(intent);
        }
        if(throwable instanceof ServerError) {
            ToastTool.showToast(throwable.getMessage());
        }else if(throwable instanceof JSONException || throwable instanceof com.alibaba.fastjson.JSONException){
            ToastTool.showToast("数据出错-1");
        }else if(throwable instanceof SocketTimeoutException){
            ToastTool.showToast("网络连接超时,请重试");
        } else if(statusCode==0) {
            ToastTool.showToast("网络连接失败");
        }
    }
    @Override
    protected T parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
        if(isFailure)
            return null;
        else
        {
            DebugHelper.i("rawJsonData", rawJsonData);
            if(rawJsonData==null || rawJsonData.equals("null")){
                return null;
            }
            JSONObject object=new JSONObject(rawJsonData);
            String status=object.getString(STATUS_KEY);
            if(TextUtils.equals(status,STATUS_SUCCESS))
            {
                return parseData(object);
            }
            else{
                String errorInfo="";
                int errorCode=0;
                if(object.has("error_info"))
                    errorInfo=object.getString("error_info");
                if(object.has("error_code"))
                    errorCode=object.getInt("error_code");
                if(errorCode==40001 || errorCode==40002)
                    throw new AuthError(errorInfo,errorCode);
                else
                    throw new ServerError(errorInfo,errorCode);
            }
        }
    }
     public abstract T parseData(JSONObject object) throws Throwable;
}
